package com.cx.update;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class MacUtil {
	
	public static String getWifiMac(Context context){
		WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		WifiInfo info = manager.getConnectionInfo();
		if(info != null){
			return info.getMacAddress();
		}
		return null;
	}
	public  static String loadFileAsString(String filePath) throws java.io.IOException{
		StringBuffer fileData = new StringBuffer(1000);
		BufferedReader reader = new BufferedReader(new FileReader(filePath));
		char[] buf = new char[1024];
		int numRead=0;
		while((numRead=reader.read(buf)) != -1){
			String readData = String.valueOf(buf, 0, numRead);
			fileData.append(readData);
		}
		reader.close();
		return fileData.toString();
	}
	public static String getMacAddress(){
		try {
			return loadFileAsString("/sys/class/net/eth0/address")
					.toUpperCase().substring(0, 17);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
	public static String getMac(){
		String macnameAddress=getMacAddress();
//         String userid=macnameAddress.toLowerCase();
//    	 Pattern p = Pattern.compile("[^0-9{a-z}]");
//   	     Matcher m = p.matcher(userid);
//   	     userid = m.replaceAll("");
		return macnameAddress;

	}
}
